import axios from "axios";
import { http } from "../utils/http";
import { BASE_URL } from "../utils/http/core";
import { ReturnDTO } from "./type";

const prefix = "/user/staff";

export interface StaffInsertDTO {
  name: string;
  role: string;
  userName: string;
  staffNumber: string;
  userPassword: string;
}

export interface StaffUpdateDTO {
  id: number;
  name: string;
  role: string;
  userName: string;
  staffNumber: string;
  isDisable: number;
}

export interface UserUpdateDTO {
  id: number;
  name: string;
  userName: string;
  staffNumber: string;
}
export interface PageStaffDTO {
  hasNext: boolean;
  hasPre: boolean;
  staffList: StaffSelectDTO[];
}

export interface StaffSelectDTO {
  id: number;
  isDisable: number | string | boolean;
  name: string;
  role: string;
  staffNumber: string;
  updateTime?: Date;
  createTime?: Date;
  userName: string;
}

// 获取登录用户信息
export const getInfo = () => {
  return http.request<ReturnDTO>("get", prefix + "/info");
};

// 修改自己的信息
export const updateSelf = (data: UserUpdateDTO) => {
  return http.request<ReturnDTO>("put", prefix + "/selfModify", data as Object);
};

// 修改自己的密码
export const modifyPwd = (pwd: string) => {
  return http.request<ReturnDTO>("put", prefix + "/modifyPwd", {
    pwd: pwd
  } as Object);
};

// 添加
export const addStaff = (data: StaffInsertDTO): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("post", prefix, data as Object);
};

// 修改
export const modifyStaff = (data: StaffUpdateDTO): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("put", prefix, data as Object);
};

// 批量添加
export const addStaffList = (data: FormData): Promise<ReturnDTO> => {
  const newRequest = axios.create({
    baseURL: BASE_URL
  });
  const info = JSON.parse(sessionStorage.getItem("info"));
  if (info) {
    return new Promise((resolve, reject) =>
      newRequest({
        method: "POST",
        url: prefix + "/addList",
        data: data,
        headers: {
          "Content-Type":
            "multipart/form-data;boundary=ebf9f03029db4c2799ae16b5428b06bd1",
          header: info.token
        }
      })
        .then((response: any) => {
          resolve(response);
        })
        .catch((error: any) => {
          reject(error);
        })
    );
  }
};

// 禁用
export const disableStaff = (staffId: number): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("get", prefix + "/disable", staffId as Object);
};

// 启用
export const enableStaff = (staffId: number): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("get", prefix + "/enable", staffId as Object);
};

// 删除
export const deleteStaff = (staffId: number): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("delete", prefix + "/" + staffId);
};

// 分页获取
export const pageStaff = (
  current: number,
  size: number
): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>("get", prefix + "/" + current + "/" + size);
};

// 分页搜索
export const searchStaff = (
  name: string,
  current: number,
  size: number
): Promise<ReturnDTO> => {
  return http.request<ReturnDTO>(
    "get",
    prefix + "/" + name + "/" + current + "/" + size
  );
};
